<template>
  <el-dialog
    title="角色权限"
    :visible="authorityShow"
    center
    @close="close"
  >
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <span slot="footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    authorityShow: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleList: [],
      checkList: []
    }
  },
  async mounted() {
    // 钩子函数
    this.roleList = (await getRoleList()).rows
  },
  methods: {
    // 点击确定
    async confirm() {
      await assignRoles({ id: this.id, roleIds: this.checkList })
      this.$message.success('修改成功')
      this.$emit('update:authorityShow', false)
    },
    // 点击取消
    close() {
      this.checkList = []
      this.$emit('update:authorityShow', false)
    },
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id)
      this.checkList = roleIds
    }
  }
}
</script>

<style lang='less' scoped>
</style>
